<?php

require_once("./modules/config.inc");
require_once("./modules/db.inc");
require_once("./modules/user.inc");
require_once("./modules/feeds/feed.inc");
require_once("./modules/feeds/feedBox.inc");
require_once("./modules/pages/loader.inc");
require_once("./modules/cache.inc");
require_once("./modules/util.inc");
require_once("./modules/IManager.inc");
require_once("./modules/blog/blogBox.inc");


class Renderer
{
	var $action;
	var $user;
	var $config;
	var $pageInfo;

	function loadUser()
	{
		if (isset($_SESSION["user"]))
			return unserialize($_SESSION["user"]);
		else
			return null;
	}

	/*do not override this function, use pageLoad instead */
	final function init()
	{
		$this->config = Config::getInstance();
		global $G_HOME;
		$G_HOME = $this->config->get('./siteUrl');

		$this->action = isset($_GET["act"]) ? $_GET["act"] : "";
		$this->user = $this->loadUser();
		
		if (!$this->isPermitted())
			header("Location: ./index.php");
	}
	
	/*
	* checks that the user is permitted to this module.
	*/
	function isPermitted()
	{
		return true;
	}
	
	/*overridable by page implementation*/
	function pageLoad()
	{
		$this->loadManualPage();
	}

	/*in case pages module is working we load the page for use of content & breadcrumbs */
	function loadManualPage()
	{
		if ($this->config->get("./features/pages/@enabled") =="1")
		{
			$page = isset($_GET['page']) ?$_GET['page'] : "";
			$this->pageInfo = PagesLoader::loadPage($page);
		}
	}
	
	/*overridable by page implementation*/
	function renderHeader()
	{
		$this->setSEODetails();
				
		global $G_HEADER;

		if ($this->config->get("./features/pages/@enabled") =="1")
		{
			$headerInfo = PagesLoader::loadPage('header');
			$G_HEADER = $headerInfo['content'];
		}
		else
			$G_HEADER = "";
	}
	
	function setSEODetails()
	{
		global $title;
		global $description;
		
		$title = "";
		$description = "";
	}
	
	final function handleBreadcrumbs()
	{
		global $breadcrumbs;

		if ($this->config->get("./features/breadcrumbs/@enabled") =="1")
			$this->renderBreadcrumbs();
	}

	/*overridable by page implementation*/
	function renderBreadcrumbs()
	{
		global $breadcrumbs;
		global $G_HOME;
		
		$siteName = $this->config->get("./siteName");
		
		$breadcrumbs = "<a href='".$G_HOME."'>".$siteName."</a>";
		
		if(isset($this->pageInfo) && $this->pageInfo['name'] != "index")
		{
			$breadcrumbs .= "&nbsp;&#187;&nbsp;".$this->pageInfo['name'];
		}
	}
	
	/*overridable by page implementation*/
	function renderContent()
	{
		global $content;
		global $title;
		global $description;
				
		if (isset($this->pageInfo))
		{
			$content = $this->loadContent($this->pageInfo["name"],$this->pageInfo["content"]);
			$title = $this->pageInfo["title"];
			$description = $this->pageInfo["desc"];
		}
		else
		{
			header("HTTP/1.0 404 Not Found");
			$content = "no content submitted";
		}
	}
	
	private function loadContent($page,$content)
	{
		//name the content type, with paging
		$cacheId = "page-content-".$page;

		$cacheStatus = 'DEAD';
		
		$cache = new Cache("",120);
		
		if( !$cache->ERROR )
			$cacheStatus = $cache->check_cache($cacheId);
		
		if($cacheStatus == 'HIT')
			$content = $cache->get($cacheId);
		else
		{
			$content = $this->loadBoxes($content);
			
			$cache->set($cacheId,$content);
		}
		
		return $content;
	}

	private function loadBoxes($content)
	{
		preg_match("/{seositer:.*?:.*?}/ims",$content,$matches);
		
		foreach($matches as $match)
		{
			list($dummy, $site, $module, $function) = preg_split('/[{:};]/', $match);
						
			$boxContent = call_user_func($module."::".$function);
			
			$content = str_replace($match,$boxContent,$content);
		}	

		return $content;
	}
	
	/*overridable by page implementation*/
	function renderLoginBox()
	{
		global $loginBox;
		global $G_HOME;
				
		$loginBox = "<div class='loginLine'>
						<ul>";
		if( isset($this->user) )
			$loginBox .= "<li class='signIn'>".$this->user->screenName."</li>
						<li><a rel='nofollow' href='".Util::combinePath($G_HOME,"index.php?pg=logout")."'>sign out</a></li>";
		else
		{
			$loginBox .= "<li class='signIn'><a rel='nofollow' href='".Util::combinePath($G_HOME,"index.php?pg=login")."'>Sign in..</a></li>";
			
			if($this->config->get("./features/registration/@enabled") == "1")
				$loginBox .="<li class='register'><a rel='nofollow' href='".Util::combinePath($G_HOME,"index.php?pg=register")."'>register</a></li>";
		}

		$loginBox .="	</ul>
					</div>";
	}
	
	/*overridable by page implementation*/
	function renderSearch()
	{
		global $G_SEARCH;
		
		if($this->config->get("./features/searchTool/@enabled") == "1")
		{
			$G_SEARCH = (string)$this->config->get("./features/searchTool/box");
		}
	}
		
	/*overridable by page implementation*/
	function renderMainNavigation()
	{
		global $mainNav;
		global $G_HOME;
		
		//name the content type, with paging
		$cacheId = "main-nav";

		$cacheStatus = 'DEAD';
		
		$cache = new Cache();
		
		if( !$cache->ERROR )
			$cacheStatus = $cache->check_cache($cacheId);
		
		//we only cache this menu for non-admins
		if($cacheStatus == 'HIT' && ( !isset($this->user) || $this->user->type != 2))
			$mainNav = $cache->get($cacheId);
		else
		{
			$mainNav = "<ul>";
			
			//rendering admin settings for admins.
			if(isset($this->user) && $this->user->type == 2)
			{
				$mainNav .= "<li class='navCategory'>Admin</li>
				<li class='navItem'><a href='".Util::combinePath($G_HOME,"index.php?pg=admin_settings")."'>Settings</a></li>";
			}
			
			//select all the features that are enabled have menu sub node.
			$features = $this->config->getConfig("./features/*[@enabled=\"1\" and menu]");
			
			foreach($features as $feature)
			{
				require_once("./modules/".(string)$feature->getName()."/_manager.inc");
				
				$attrs = $feature->attributes();
				$manager = (string)$attrs["manager"];				
				$managerInstance = new $manager;
				
				if(isset($this->user) && $this->user->type >= 1 &&
					(string)$feature->menu->admin =="1")
					$mainNav .= $managerInstance->getAdminMenu();
				
				if( (string)$feature->menu->user =="1")
					$mainNav .= $managerInstance->getMenu();
			}
				
			$mainNav .= "</ul>";
			
			if ( !isset($this->user) || $this->user->type != 2)
				$cache->set($cacheId,$mainNav);
		}
	}
	
	function renderLocalNavigation()
	{
		global $secondaryNavigation;
		
		$secondaryNavigation = "";
	}
	
	/*overridable by page implementation*/
	function renderFooter()
	{
		global $G_FOOTER;

		if ($this->config->get("./features/pages/@enabled") =="1")
		{
			$footerInfo = PagesLoader::loadPage('footer');
			$G_FOOTER = $footerInfo['content'];
		}
		else
			$G_FOOTER = "default footer";
	}
	
	function renderAnalytics()
	{
		global $analytics;
		
		$analytics = $this->config->get("./analytics");
	}
}
$renderer = new Renderer();

?>